package com.caigou.comm.base.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.caigou.comm.R

/**
 * @author #Suyghur.
 * Created on 2021/09/26
 */
class RecyclerViewDividerDecoration(context: Context) : RecyclerView.ItemDecoration() {
    private var mDivider: Drawable? = null

    init {
        mDivider = ColorDrawable(context.resources.getColor(R.color.caigou_gray_50))
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        drawHorizontal(canvas, parent)
        drawVertical(canvas, parent)
    }

    override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView) {
        val spanCount = getSpanCount(parent)
//        val childCount = parent.adapter!!.itemCount
//        if (isLastColum(parent, itemPosition, spanCount)) {
//            Logger.d("最后一列")
////            if (itemPosition == (childCount - 1)) {
////                Logger.d("(itemPosition == (childCount - 1))")
////                outRect.set(0, 0, 0, 0)
////            } else {
////                outRect.set(0, 0, 0, mDivider!!.intrinsicHeight)
////            }
//            outRect.set(0, 0, 0, 0)
//        } else if (isLastRow(parent, itemPosition, spanCount)) {
//            // 如果是最后一行，则不用绘制底部
//            outRect.set(0, 0, mDivider!!.intrinsicWidth, 0)
//        } else {
//            outRect.set(0, 0, mDivider!!.intrinsicWidth, mDivider!!.intrinsicHeight)
//        }
        outRect.set(0, 0, 0, 0)
    }

    private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.top - params.topMargin
            val bottom = child.bottom - params.bottomMargin
            val left = child.right + params.rightMargin
            val right = left + mDivider!!.intrinsicWidth

            mDivider!!.setBounds(left, top, right, bottom)
            mDivider!!.draw(canvas)
        }
    }

    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom - params.bottomMargin
            val bottom = top + mDivider!!.intrinsicHeight
            val left = child.left + params.leftMargin
            val right = child.right + params.rightMargin + mDivider!!.intrinsicWidth
            mDivider!!.setBounds(left, top, right, bottom)
            mDivider!!.draw(canvas)
        }
    }


    private fun getSpanCount(parent: RecyclerView): Int {
        // 列数
        val spanCount = when (val layoutManager = parent.layoutManager) {
            is GridLayoutManager -> {
                layoutManager.spanCount
            }
            is StaggeredGridLayoutManager -> {
                layoutManager.spanCount
            }
            else -> {
                -1
            }
        }
        return spanCount
    }


    private fun isLastColum(parent: RecyclerView, pos: Int, spanCount: Int): Boolean {
        val layoutManager = parent.layoutManager
        var childCount = parent.adapter!!.itemCount
        if (layoutManager is GridLayoutManager) {
            if ((pos + 1) % spanCount == 0) {
                // 如果是最后一列，则不需要绘制右边
                return true
            }
        } else if (layoutManager is StaggeredGridLayoutManager) {
            val orientation = layoutManager.orientation
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0) {
                    // 如果是最后一列，则不需要绘制右边
                    return true
                }
            } else {
                // 如果是最后一列，则不需要绘制右边
                childCount -= childCount % spanCount
                return pos >= childCount
            }
        }
        return false
    }

    private fun isLastRow(parent: RecyclerView, pos: Int, spanCount: Int): Boolean {
        val layoutManager = parent.layoutManager
        var childCount = parent.adapter!!.itemCount
        if (layoutManager is GridLayoutManager) {
            var last = childCount % spanCount
            if (last == 0) {
                last = spanCount
            }
            childCount -= last
            return pos >= childCount
        } else if (layoutManager is StaggeredGridLayoutManager) {
            val orientation = layoutManager.orientation
            return if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                var last = childCount % spanCount
                if (last == 0) {
                    last = spanCount
                }
                childCount -= last
                pos >= childCount
            } else {
                (pos + 1) % spanCount == 0
            }
        }
        return false
    }


}